const { query, param } = require("express-validator");
const mongoose = require("mongoose");

const pageno = query("pageno")
    .default(1)
    .isInt({ min: 1 })
    .withMessage("页码必须为大于0的整数")
    .toInt();

const pagesize = query("pagesize")
    .default(10)
    .isInt({ min: 1 })
    .withMessage("页大小必须为大于1的整数")
    .toInt();

const mongoid = param("id").custom((value) => {
    if (!mongoose.Types.ObjectId.isValid(value)) {
        throw new Error("id不合法");
    }
    return true;
});

module.exports = { pageno, pagesize, mongoid };
